<!DOCTYPE html>
<html lang="zh-cmn-Hans">

	<head>
		<meta http-equiv="Expires" content="0" />
		<meta http-equiv="Pragma" content="no-cache" />
		<meta http-equiv="Cache-control" content="no-cache" />
		<meta http-equiv="Cache" content="no-cache" />
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
		<title>光耦系列产品生产流程</title>
		<!--加载layui样式-->
		<link rel="stylesheet" href="../layui/css/layui.css">
	</head>

	<body>
		<fieldset class="layui-elem-field site-demo-button">
			<legend><big><b>光耦系列产品生产流程</b></big></legend>
			<div class="layui-fluid layui-form layui-form-pane">
				<div class="layui-row layui-form-item">
					<label class="layui-form-label">扫描二维码:</label>
					<div class="layui-input-block">
						<input class="layui-input" placeholder="请扫入特定的二维码" id="qr" type="text" />
					</div>
				</div>
			</div>
		</fieldset>

		<fieldset class="layui-elem-field site-demo-button">
			<legend><big><b>这里是当前扫描的信息组合</b></big></legend>
			<div id="DQ" class="layui-fluid layui-form layui-form-pane">
				<div class="layui-col-sm4">
					<label class="layui-form-label">员工工号:</label>
					<div class="layui-input-block">
						<input class="layui-input" readonly=true placeholder="这里是员工工号" id="YGGH" type="text" />
					</div>
				</div>

				<div class="layui-col-sm4">
					<label class="layui-form-label">员工姓名:</label>
					<div class="layui-input-block">
						<input class="layui-input" readonly=true placeholder="这里是员工姓名" id="YGXM" type="text" />
					</div>
				</div>

				<div class="layui-row">
					<div class="layui-col-sm4">
						<label class="layui-form-label">当前机台:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是当前机台" id="DQJT" type="text" />
						</div>
					</div>

					<div class="layui-col-sm4">
						<label class="layui-form-label">工单号:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是工单号" id="GDH" type="text" />
						</div>
					</div>
				</div>

			</div>
		</fieldset>

		<fieldset class="layui-elem-field site-demo-button">
			<legend><big><b>当前工序填写的内容</b></big></legend>
			<form id="TXQ" class="layui-fluid layui-form layui-form-pane" lay-filter='test1'>
			</form>
			<div class="layui-row layui-btn-container">

			</div>
		</fieldset>

		<!-- <div class="layui-tab layui-tab-card" lay-filter="demo"> -->
			<!-- <fieldset class="layui-elem-field site-demo-button layui-btn-container"> -->
				<!-- <!-- 手风琴测试区 --> -->
				<!-- <div class="layui-collapse" lay-accordion> -->
					<!-- <div class="layui-colla-item"> -->
						<!-- <h2 class="layui-colla-title">点击查看该工单其它流程单</h2> -->
						<!-- <div class="layui-colla-content" id="wospe"> -->
							<!-- <!-- 这里是需要打印的区域,也就是二维码区域 --> -->
							<!-- <div align="left" class="layui-row"> -->
								<!-- <div id="YCQ" class="layui-row layui-btn-container"></div> -->
							<!-- </div> -->
						<!-- </div> -->
					<!-- </div> -->
				<!-- </div> -->
				<!-- <!-- 手风琴结尾 --> -->
			<!-- </fieldset> -->
		<!-- </div> -->

		<div class="layui-tab layui-tab-card" lay-filter="demo">
			<fieldset class="layui-elem-field site-demo-button layui-btn-container">
				<legend><big><b>组装规格书详情内容</b></big></legend>
				<div class="layui-fluid layui-form layui-form-pane">
					<div id="GGXQ" class="layui-row layui-btn-container"></div>
				</div>
			</fieldset>
		</div>

		<!-- 加载jQuery文件 -->
		<script src="../js/jquery.min.js"></script>
		<!-- 加载类ui文件 -->
		<script src="../layui/layui.js"></script>
		<!-- 写脚本 -->
		<script>
			var GD = ""; //全局工单
			var QTY = 0; //全局基准片数
			var xuhao = ""; //全局工单序号

			var JT2 = {}; //机台的字典: 存扫描的次数 i
			var i = 0; //全局给个自增值
			var JTGD = {}; //这里是机台号对应工单号的数组
			var ZuZhuangZDArr = {}; //组装规格书的对象数组
			var ZuZhuangZDArr1 = {}; //组装规格书的对象数组

			//载入layui模块：layer弹出层,form表单,element元素组件做分页或其他
			layui.use(['jquery', 'layer', 'form', 'element'], function() {
				var $ = layui.$; //重点处
				var layer = layui.layer;
				var form = layui.form;
				var element = layui.element;
				var table = layui.table;

				//这里是画面准备完毕后执行的程式
				$(document).ready(function() {
					//如果HTML5WEB存储存在,就查询,否则就弹窗提示
					if(localStorage.scyggh) {
						$("#YGGH").val(localStorage.scyggh);
						$.ajax({
							type: 'POST',
							url: "http://127.0.0.1:85/index.php?s=/scancode/Product/xm", //172.31.3.27:84//127.0.0.1:85,//POST傳一個超長字串到後端
							data: "qrc=" + localStorage.scyggh,
							dataType: "json",
							success: function(sss) {
								if(sss.msg) {
									layer.msg(sss.msg);
								}

								layer.msg(sss['XM'] + ',欢迎回来!');
								$("#YGXM").val(sss['XM']); //把姓名单独查出来
							}
						});

					} else {
						layer.msg("请扫工号牌");
					}

					$("#qr").select(); //画面准备完毕后就选择扫码输入框

				});

				//这里是扫描输入框的回车按下后执行的程式
				$("#qr").keypress(function(e) {
					//如果按下的键值是回车键13就执行以下语句
					if(e.keyCode === 13) {
						//扫描输入框选择
						$("#qr").select();
						//定义局部变量QRC=扫描输入框的值
						var QRC = $("#qr").val();

						/*
						TAAK17B2681-2 T2865811K-Q4801A 10 002
						编带站的正则测试
						11 11.7 2.7 2.8 C22 U0C22Y1 80 90 65 BPI31798706019-1 AK187029C-X T2850812K-Q31B3A 8
						http://tool.oschina.net/regex测试通过
						*/
						var patt1 = /^[A-Z]{1,2}[A-Z\d]*\-?[A-Z\d]?(?= T)/; //AK17B2681-2 -> mg组装规格书工单号
						var patt2 = /\d{3}$/; //002组装规格书盒序号
						var patt3 = /[A-Z]{1,2}[A-Z\d]*\-?[A-Z\d]?(?= T)/; //AK187029C-X分光标签工单号
						var patt4 = /BP[A-Z][A-Z\d]*\-?[A-Z\d]*(?= [A-Z]{1,2})/; //BPI31798706019-1分光标签序号(用作唯一码)
						var patt5 = /BP[A-Z]\d{4}/; //机台号或MES机台EQ
						var patt6 = /\d+(?= \d{3}$)/; //组装规格书基准片数
						var patt7 = /^TA/; //兼容,还是要替换掉TA
						var patt8 = /T.*?(?= \d{2} \d{3}$)/; //判断料号 T2865811K-Q4801A
						var patt9 = /^EM\d{5}$/; //判断工号

						var ZJ = /^XMJ\d{10}$/; //1支架
						if($("#DID").val() == 1 && ZJ.test(QRC)) {
							//console.log(QRC);
							$("[data-dd='支架批次号']").val(QRC); //把xx的lot号打上去
							//console.log($("[data-dd='支架批次号']").val());
							return;
						}

						var LED = /^T1A\d{9}\-\d{2}\-\d{2}$/; //2LED
						if($("#DID").val() == 1 && LED.test(QRC)) {
							$("[data-dd='LED批次号']").val(QRC); //把xx的lot号打上去
							return;
						}

						var YJ = /^\d{7}W$/; //3银胶
						if($("#DID").val() == 1 && YJ.test(QRC)) {
							$("[data-dd='银胶批次号']").val(QRC); //把xx的lot号打上去
							return;
						}

						var PT = /^0468[A-Z]{2}\-\d{4}/; //4PT
						if($("#DID").val() == 3 && PT.test(QRC)) {
							$("[data-dd='PT批次号']").val(QRC); //把xx的lot号打上去
							return;
						}

						var YJ = /^\d{7}W$/; //5银胶
						if($("#DID").val() == 3 && YJ.test(QRC)) {
							$("[data-dd='银胶批次号']").val(QRC); //把xx的lot号打上去
							return;
						}

						var JX = /^\d{7}AW\d{2}$/; //6金线
						if($("#DID").val() == 6 && JX.test(QRC)) {
							$("[data-dd='线材批次号']").val(QRC); //把xx的lot号打上去
							return;
						}

						var GuiJ = /^[A-Z\d]{10}$/; //7硅胶
						if($("#DID").val() == 7 && GuiJ.test(QRC)) {
							$("[data-dd='硅胶批次号']").val(QRC); //把xx的lot号打上去
							return;
						}

						var YDJ = /^[A-Z\d]{6}$/; //8一道胶
						if($("#DID").val() == 10 && YDJ.test(QRC)) {
							$("[data-dd='一道胶饼批次号']").val(QRC); //把xx的lot号打上去
							return;
						}

						var EDJ = /^[A-Z\d]{6}$/; //9二道胶
						if($("#DID").val() == 12 && EDJ.test(QRC)) {
							$("[data-dd='二道胶饼批次号']").val(QRC); //把xx的lot号打上去
							return;
						}

						var cpxhstr = QRC.replace(/^TA.*?(?= T)/, ""); //替换掉开头的【TAAK17B2681-2】还留了个空格在头

						var string1 = QRC.replace(patt7, ""); //清除开头的TA

						if(patt1.test(string1) || patt3.test(string1)) { //判断是否为工单号
							if(patt1.test(string1)) { //组装规格书工单号测试通过
								GD = patt1.exec(string1) + ""; //取出纯工单号
								QTY = patt6.exec(string1) + ""; //全局基准片数
								xuhao = patt2.exec(string1) + ""; //序号

								var string2 = GD + "-" + xuhao; //取出工单号盒号
								$("#GDH").val(string2); //写分光标签的工单号

								JTGD[$("#DQJT").val()] = string2; //机台工单索引机台=工单号盒号
								if($("#DQJT").val() !== "") { //如果当前机台不为空,就调用zggs
									//确认工单号和机台号没问题了就开始调用函数了
									console.log("请开始你的表演");
									zzggs();
								} else {
									layer.msg('机台号不能为空');
								}

							} else if(patt3.test(QRC)) { //否则它是分光标签
								var string2 = patt3.exec(QRC) + "-" + patt4.exec(QRC); //取出分光标签的工单号
								$("#GDH").val(string2); //写分光标签的工单号
								JTGD[$("#DQJT").val()] = string2; //写当前机台的工单号
							} else {
								layer.msg('工单号的正则不通过，请检查！<br/>组装规格书是否是TA开头？<br/>编带站卷盘标签是否包含工单号');
							}
						} else if(patt5.test(QRC)) { //取到工单号就不会跑来取机台号了吧,编带机上面的流水码是BPI分光机自动标签
							var dqjt = patt5.exec(QRC); //当前机台

							var string3 = ""; //前三码文本

							string3 = dqjt[0].substr(0, 3); //文本3等于当前机台的前三码(待测)

							$("#DQJT").val(dqjt[0]); //写当前机台
							JTGD[dqjt[0]] = $("#GDH").val(); //当前机台的工单号

						} else if(patt9.test(QRC)) {
							localStorage.scyggh = QRC;
							location.reload(); //如果是工号,还要清除字典内容,重新来过...所以干脆刷新一下好了
						} else {
							layer.msg('无法被识别的条码<br/>或<br/>员工工号、当前机台和工单号不允许为空');
						}
					}
				});

			}); //layui尾巴

			$("#qr").click(function() {
				$("#qr").select();
			});

			function zzggs() { //显示当前工单号的所有组装规格书列表
				ZuZhuangZD = {}; //清空组装字典

				Dhtml = ""; //动态身体画面

				$.ajax({
					type: 'POST',
					url: "http://127.0.0.1:85/index.php?s=/scancode/hongwai/H", //172.31.3.27:84//127.0.0.1:85
					data: "GDH=" + $("#GDH").val(),
					dataType: "json",
					success: function(sss) {
						if(sss.msg) {
							layer.msg(sss.msg);
						}
						if(sss.CD >= 1) {
							$("#YCQ").html("");

							for(k in sss.ZZGG) {
								var hid = sss['ZZGG'][k]['THID']; //交易表头ID
								var gdh = sss['ZZGG'][k]['GDH']; //工单号+盘序号
								var title = sss['ZZGG'][k]['title']; //标题
								var series = sss['ZZGG'][k]['series']; //光耦系列

								ZuZhuangZDArr[hid] = [hid, gdh, series, title]; //组装规格书字典数组

								if(gdh == $("#GDH").val()) { //如果盒序号等于当前盒,就变颜色
									ZuZhuangZD[hid] = hid + '丨工单号：' + gdh + '丨规格书名称：' + title + '丨产品系列：' + series;
									html = "<button id='" + hid + "' class='layui-btn layui-btn-warm layui-btn-fluid' data-d='" + hid + "' data-gdh='" + gdh + "' onclick='showZZGG(this)' style='text-align:left'>" + ZuZhuangZD[hid] + "</button>";
									$("#YCQ").append(html);
								} else {
									ZuZhuangZD[hid] = hid + '丨工单号：' + gdh + '丨规格书名称：' + title + '丨产品系列：' + series;
									html = "<button id='" + hid + "' class='layui-btn layui-btn-normal layui-btn-fluid' data-d='" + hid + "' data-gdh='" + gdh + "' onclick='showZZGG(this)' style='text-align:left'>" + ZuZhuangZD[hid] + "</button>";
									$("#YCQ").append(html);
								}
							}
						}
						tesapi(); //调用写工序
					}
				});
			}

			function tesapi() {
				ZuZhuangZD1 = {};

				$.ajax({
					type: 'POST',
					url: "http://127.0.0.1:85/index.php?s=/scancode/hongwai/gx", //172.31.3.27:84//127.0.0.1:85
					data: "GDH=" + $("#GDH").val(),
					dataType: "json",
					success: function(sss) {
						var testid = 0;
						$("#GGXQ").html("");
						for(k in sss.ZZGGXQ) {
							var zzz = sss['ZZGGXQ'][k]['THID']; //交易档头id
							var did = sss['ZZGGXQ'][k]['did']; //基础档身id
							var seq = sss['ZZGGXQ'][k]['seq']; //工序序号
							var workseq = sss['ZZGGXQ'][k]['workseq']; //工序名
							var JTH = sss['ZZGGXQ'][k]['JTH']; //机台号
							var lpbh = sss['ZZGGXQ'][k]['PAN_ID']; //料盘唯一编号
							var zyy = sss['ZZGGXQ'][k]['YGGH']; //作业员
							var xm = sss['ZZGGXQ'][k]['Name']; //作业员
							var trs = sss['ZZGGXQ'][k]['TRS']; //投入数量
							var wcs = sss['ZZGGXQ'][k]['WCS']; //完成数量
							ZuZhuangZDArr1[did] = [zzz, workseq, lpbh]; //组装规格书字典,给其它按钮function使用

							if(zyy) { //如果有作业员,就认为是已经被报工了,要判断是不是报完,没报完可以继续报
								ZuZhuangZD1[did] = seq + '：' + workseq + '丨机台号：' + JTH + '丨料盘唯一编号：' + lpbh + '丨作业员：' + zyy + '丨' + xm + '丨投入数' + trs + '丨完成数' + wcs;
								html = "<button style='text-align:left' id='" + zzz + "' class='layui-btn layui-btn-warm layui-btn-fluid' data-d='" + did + "' data-lpbh='"+lpbh+"' onclick='showTb(this)'>" + ZuZhuangZD1[did] + "</button>";
								$("#GGXQ").append(html);
								//$("#" + zzz).attr('disabled', true); //禁用该按钮
							} else {
								ZuZhuangZD1[did] = seq + '：' + workseq;
								html = "<button style='text-align:left' id='" + zzz + "' class='layui-btn layui-btn-fluid' data-d='" + did + "'onclick='showTb(this)'>" + ZuZhuangZD1[did] + "</button>";
								$("#GGXQ").append(html);
							}
						}
					}
				});
			}

			function showZZGG(data) {
				ZuZhuangZD1 = {};
				var d1 = data.getAttribute("data-d");
				var gdh = data.getAttribute("data-gdh");
				//data.disabled = true; //按钮被禁用
				data.className = 'layui-btn  layui-btn-warm layui-btn-fluid'; //添加xx渲染
				$("#GDH").val(gdh); //点击后,修改工单号内容

				tesapi(); //这里有坑
			}

			function showTb(data) {
				var thid = data.getAttribute("id");
				var d1 = data.getAttribute("data-d");
				var lpbh = data.getAttribute("data-lpbh");

				data.className = 'layui-btn layui-btn-warm layui-btn-fluid'; //添加颜色标识当前点击

				$.ajax({
					type: 'POST',
					url: "http://127.0.0.1:85/index.php?s=/scancode/hongwai/dd", //172.31.3.27:84//127.0.0.1:85
					data: "did=" + d1 + "&GDH=" + $("#GDH").val(),
					dataType: "json",
					success: function(sss) {
						if(sss.msg) {
							layer.msg(sss.msg);
						}

						$("#TXQ").html(""); //填写区的画面清空,然后重新动态产生
						Hhtml = "<input id='DID' type='hidden' name='DID' required class='layui-input'/>" + //隐藏一个DID
							"<input id='THID' type='hidden' name='THID' required class='layui-input'/>" + //隐藏一个THID
							"<input type='hidden' name='JTH' required class='layui-input'/>" + //隐藏一个机台
							"<input type='hidden' name='YGGH' required class='layui-input'/>" + //隐藏一个员工
							"<input type='hidden' name='GDH' required class='layui-input'/>" + //隐藏一个工单
							"<div class='layui-form-item layui-form-text'>" +
							"	<label class='layui-form-label'>料盘唯一编号</label>" +
							"	<input type='TEXT' id='PAN' name='PAN_ID' required lay-verify='required' placeholder='请输入料盘唯一编号' autocomplete='off' class='layui-input'/>" + //开放一个盘唯一编码
							"</div>";
						$("#TXQ").append(Hhtml); //把画面打印上去
						for(k in sss.GXXQ) {
							if(sss.CD >= 1) {
								DDhtml = "<div class='layui-form-item layui-form-text'>" +
									"	<label class='layui-form-label'>" + sss.GXXQ[k]['SAVKEY'] + "</label>" +
									"	<div class='layui-input-block'>" +
									"		<input type='text' name='" +k + "' required  lay-verify='required' placeholder='请输入" + sss.GXXQ[k]['SAVKEY'] + "' autocomplete='off' data-dd='" + sss.GXXQ[k]['SAVKEY'] + "' class='layui-input' value='"+sss.GXXQ[k]['value']+"' />" +
									"	</div>" +
									"</div>";
							} else {
								DDhtml = "<div class='layui-form-item layui-form-text'>" +
									"	<label class='layui-form-label'>" + sss.GXXQ[k]['SAVKEY'] + "</label>" +
									"	<div class='layui-input-block'>" +
									"		<input type='text' name='" + k + "' required  lay-verify='required' placeholder='请输入" + sss.GXXQ[k]['SAVKEY'] + "' autocomplete='off' data-dd='" + sss.GXXQ[k]['SAVKEY'] + "' class='layui-input'/>" +
									"	</div>" +
									"</div>";
							}
							$("#TXQ").append(DDhtml); //把画面打印上去

						}

						$("[data-dd='投入数量LF']").val(QTY); //全局基准片数作为投入片数
						$("#PAN").val(lpbh); //如果有数据就把盘唯一编码打上去
						DDhtml = "<div class='layui-form-item layui-form-text'>" +
							"	<label class='layui-form-label'>备注</label>" +
							"	<textarea type='TEXT' name='BZ' placeholder='请输入备注信息' class='layui-textarea'></textarea >" + //开放一个备注
							"</div>";
						$("#TXQ").append(DDhtml); //把备注+按钮打印上去
						if(!/^\d+$/.test(thid)) {
							$("#TXQ").append("<div class='layui-row'><button class='layui-btn  layui-btn-normal' id='keep'  lay-submit lay-filter='formDemo' >保存</button></div>"); //判断开放一个保存按钮
						}

						layui.use(['form'], function() {
							var form = layui.form;
							form.render(null, 'test1'); //更新 lay-filter="test1" 所在容器内的全部表单状态

							//test1 即 class="layui-form" 所在元素对应的 lay-filter="" 对应的值
							form.val("test1", {
								"DID": d1, // "name": "value"
								"THID": thid,
								"JTH": $("#DQJT").val(),
								"YGGH": $("#YGGH").val(),
								"GDH": $("#GDH").val(),
								"BZ": sss.BZ,
							});

							//监听提交
							form.on('submit(formDemo)', function(data) {
								var jsd = JSON.stringify(data.field)
								console.log(jsd);
								layer.msg(jsd); //jsondata
								$.ajax({ //发送到后端做保存动作
									type: 'POST',
									url: "http://127.0.0.1:85/index.php?s=/scancode/hongwai/keep2mode", //172.31.3.27:84//127.0.0.1:85,//POST傳一個超長字串到後端
									data: "jsd=" + jsd,
									dataType: "json",
									success: function(sss) {
										if(sss.msg == '保存成功') {
											layer.msg(sss.msg);
											$("[data-d='" + sss.DID + "']").attr('id',sss.THID); //全局基准片数作为投入片数
											console.log($("[data-d='" + sss.DID + "']").attr('id'));
											$("#TXQ").html(""); //保存成功就把填写区的画面清空
											tesapi(); //调用写工序
										}
									}
								});
								return false; //禁止form跳转
							});
						});
					}
				});

				$("#qr").select(); //先选择扫码框,支援扫七大材料
				$("html,body").animate({
					scrollTop: $("#TXQ").offset().top
				}, 500);
			}
		</script>
	</body>

</html>